package com.sicpa.system.service.impl;

import com.sicpa.common.core.domain.entity.SysUser;
import com.sicpa.common.core.domain.model.LoginUser;
import com.sicpa.common.utils.SecurityUtils;
import com.sicpa.system.service.ICheckUserPowerService;
import com.sicpa.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Set;

/**
 * CheckUserPowerImpl
 * @author lwf
 */
@Service
public class CheckUserPowerServiceImpl implements ICheckUserPowerService {

    private ISysRoleService iSysRoleService;

    @Autowired
    public CheckUserPowerServiceImpl(ISysRoleService iSysRoleService) {
        this.iSysRoleService = iSysRoleService;
    }

    /**
     * 判断当前登录用户是否拥有某个角色
     * @param roleKey
     * @return
     */
    @Override
    public boolean hasRole(String roleKey){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        // 超级管理员拥有所有角色
        if (user.isAdmin()){
            return true;
        }
        Set<String> roles = iSysRoleService.selectRolePermissionByUserId(user.getUserId());
        return roles.contains(roleKey);
    }

    /**
     * 非超级管理员判断角色
     * @param roleKey
     * @return
     */
    @Override
    public boolean noAdminHashRole(String roleKey){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        Set<String> roles = iSysRoleService.selectRolePermissionByUserId(user.getUserId());
        return roles.contains(roleKey);
    }

}
